﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.ximpleware;
using System.Xml;

namespace Visualization.DataModel
{
    public class ParticleProvider : IItemsProvider<SFPStep>
    {
        //VTDGen gen = new VTDGen();
        string file;

        public ParticleProvider(string fileName)
        {
            file = fileName;
           // gen.parseFile(fileName, false);
        }

        #region IItemsProvider<SFPStep> Members

        public int FetchCount()
        {
            return 20; // I don't know what this does right now....
        }

        public IList<SFPStep> FetchRange(int startIndex, int count)
        {
            return bind(startIndex, count);
        }

        private List<SFPStep> bind(int startIndex, int count)
        {
            List<SFPStep> steps = new List<SFPStep>();
            XmlTextReader reader = new XmlTextReader(file);
            reader.ReadOuterXml();
            reader.ReadStartElement("SFPSteps");
            while (reader.Read())
            {
                int check = Convert.ToInt32(reader.GetAttribute("Step"));
                if (check >= startIndex && check <= (startIndex + count))
                {
                    SFPStep step = new SFPStep();
                    step.Step = Convert.ToInt32(reader.GetAttribute("Step"));
                    step.X = Convert.ToDouble(reader.GetAttribute("X"));
                    step.Y = Convert.ToDouble(reader.GetAttribute("Y"));
                    step.Theta = Convert.ToDouble(reader.GetAttribute("Theta"));
                    step.RefTime = Convert.ToDouble(reader.GetAttribute("RefTime"));
                    step.HighestProbabilityParticle = new Particle()
                    {
                        X = Convert.ToDouble(reader.GetAttribute("HX")),
                        Y = Convert.ToDouble(reader.GetAttribute("HY")),
                        Theta = Convert.ToDouble(reader.GetAttribute("HT")),
                        Probability = Convert.ToDouble(reader.GetAttribute("HP"))
                    };
                    step.xBar = Convert.ToDouble(reader.GetAttribute("XBar"));
                    step.xSigma = Convert.ToDouble(reader.GetAttribute("XSigma"));
                    step.yBar = Convert.ToDouble(reader.GetAttribute("YBar"));
                    step.ySigma = Convert.ToDouble(reader.GetAttribute("YSigma"));
                    step.thetaBar = Convert.ToDouble(reader.GetAttribute("ThetaBar"));
                    step.thetaSigma = Convert.ToDouble(reader.GetAttribute("ThetaSigma"));
                    step.particles = new List<Particle>();
                    XmlReader particleReader = reader.ReadSubtree();
                    int i = 0;
                    while (particleReader.Read())
                    {
                        i++;
                        Particle p = new Particle();
                        p.X = Convert.ToDouble(reader.GetAttribute("X"));
                        p.Y = Convert.ToDouble(reader.GetAttribute("Y"));
                        p.Theta = Convert.ToDouble(reader.GetAttribute("Theta"));
                        p.Probability = Convert.ToDouble(reader.GetAttribute("Probability"));
                        step.particles.Add(p);
                        particleReader.Read();
                    }
                    steps.Add(step);
                    if (check == count)
                        break;
                }
            }
            reader.Close();
            /*
            AutoPilot stepAP = new AutoPilot();
            AutoPilot stepXAP = new AutoPilot();
            AutoPilot stepYAP = new AutoPilot();
            AutoPilot stepTAP = new AutoPilot();
            AutoPilot stepRAP = new AutoPilot();
            AutoPilot partAP = new AutoPilot();
            AutoPilot partXAP = new AutoPilot();
            AutoPilot partYAP = new AutoPilot();
            AutoPilot partTAP = new AutoPilot();
            AutoPilot partPAP = new AutoPilot();
            stepAP.selectXPath("/SFPSteps/Step[@Step > " + startIndex + " and @Step < " + (startIndex + count) + "]");
            stepXAP.selectXPath("@X");
            stepYAP.selectXPath("@Y");
            stepTAP.selectXPath("@Theta");
            stepRAP.selectXPath("@RefTime");
            partPAP.selectXPath("Particles[Particle]");
            partXAP.selectXPath("Particles/Particle/X");
            partYAP.selectXPath("Particles/Particle/Y");
            partTAP.selectXPath("Particles/Particle/Theta");
            partPAP.selectXPath("Particles/Particle/Probability");
            stepAP.bind(gen.getNav());
            stepXAP.bind(gen.getNav());
            stepYAP.bind(gen.getNav());
            stepTAP.bind(gen.getNav());
            stepRAP.bind(gen.getNav());
            partXAP.bind(gen.getNav());
            partYAP.bind(gen.getNav());
            partTAP.bind(gen.getNav());
            partPAP.bind(gen.getNav());
            while (stepAP.evalXPath() != -1)
            {
                SFPStep step = new SFPStep();
                step.X = stepXAP.evalXPathToNumber();
                step.Y = stepYAP.evalXPathToNumber();
                step.Theta = stepTAP.evalXPathToNumber();
                while (partAP.evalXPath() != -1)
                {
                    Particle ptcle = new Particle();
                    ptcle.X = partXAP.evalXPathToNumber();
                    ptcle.Y = partYAP.evalXPathToNumber();
                    ptcle.Theta = partTAP.evalXPathToNumber();
                    ptcle.Probability = partPAP.evalXPathToNumber();
                    step.particles.Add(ptcle);
                }
                steps.Add(step);
            }
            */
            return steps;
        }

        #endregion
    }
}
